$(function () {

	var alldata = new Array();

	var num = alldata.length - 1;
	var show = $("#image");
	var btn = $("#choujiangstart");
	var open = false;

	function change() {
		//获取数组的数值范围和概率
		var randomVal = Math.round(Math.random() * num);
		
		var prizeSrc = alldata[randomVal]['user_headimgurl'];
		var prizeName = alldata[randomVal]['user_nickname'];
		//show.text(prizeName);
		show.attr("src", prizeSrc);
		$('.nicheng-box').text(prizeName);
		show.attr("index", randomVal);

		if(!open){

			stopRun();

		}
	}

	Array.prototype.indexOf = function (val) {
		for (var i = 0; i < this.length; i++) {
			if (this[i]['user_headimgurl'] == val) return i;
		}
		return -1;
	};

	Array.prototype.remove = function (val) {
		var index = this.indexOf(val);
		if (index > -1) {
			this.splice(index, 1);
		}
	};

	function run() {
		if (!open) {
			var oAudio = document.getElementById('audio');
            oAudio.pause();
			document.getElementById("image").src="../../../../Public/libs/nianhui1/image/kaishi.png";
    		$("#show").css({
    			"width":"660px",
    			"height":"500px",
    			"background":"url(../../../../Public/libs/nianhui1/img/choujiang1.png) no-repeat",
    			"background-size":"100%",
    			"line-height":"50px",
    			"text-align":"center",
    			"color":"#fff",
    			"font-size":"20px",
				"padding-top":"235px"
    		});
			
			$("#image").css({
				"width": "240px",
				"height": "191px",
				"border-radius":"0px"	
			});
			$("#zjrnc").hide();
			$("#c").hide();

			alldata=$.allUser;
			num = alldata.length - 1;
			
			

			if(alldata.length==0){
				alert('没有用户啦');
				return ;
			}

			timer = setInterval(change, 20);
			btn.removeClass('start').addClass('stop').text('停止');
			open = true;
		} else {

			clearInterval(timer);
			btn.removeClass('stop').addClass('start').text('开始');
			open = false;
		}
	}


	function stopRun(){

		var oAudio = document.getElementById('audio');
            oAudio.play();
			
			$("#show").css({
				"width":"660px",
    			"height":"500px",
				"background":"url(../../../../Public/libs/nianhui1/img/choujiangkuang.png) no-repeat center",
				"background-size":"100%",
    			"line-height":"50px",
    			"text-align":"center",
    			"color":"#fff",
    			"font-size":"20px",
				"padding-top":"135px"
			});
			$("#image").css({
				"width": "240px",
				"height": "250px",
				"border-radius":"10px"
				
			});
			$("#zjrnc").show();
			$("#c").show();

			//attr获取属性src的值
			var name = $("#image").attr("src");

			

			var index=alldata.indexOf(name);

			if(store.get('cj__iswinning_recorder')){
				var obj=store.get('cj__iswinning_recorder');
			}else{
				var obj={};
			}

			obj[alldata[index]['user_openid']]=true;
			

			store.set('cj__iswinning_recorder',obj);


			// 提交中奖的用户信息
			$.ajax({
                type: 'POST',
                url: '/admin.php/Active/savedrawuser',
                data: {
                    openid:alldata[index]['user_openid'],
                    drawName: '用户中奖啦'
                },
                success: function (data) {
					console.log(data);
					if(alldata.length>=1){
						alldata.remove(name);		
					}
                },
                error: function (err) {
                    console.log(err);
                }
            })





	}
	btn.click(function () {
		run();
	})
	$(document).keydown(function (e) {
		if (!e) var e = window.event;
		if (e.keyCode == 32) {
			run();
		}
	});

})